package com.huikeportal.service.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huikeportal.dao.DataTransInfoMapper;
import com.huikeportal.dao.PosApplicationMapper;
import com.huikeportal.model.DataTransInfo;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.PosApplication;
import com.huikeportal.model.SiteAllInfo;
import com.huikeportal.model.TableColumnInfo;
import com.huikeportal.model.TransObject;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.WordBook;
import com.huikeportal.model.common.PageDataVo;
import com.huikeportal.service.DataTransitionService;


@Service("dataTransitionService")
public class DataTransitionServiceImpl implements DataTransitionService {

	@Autowired
	private DataTransInfoMapper dataTransInfoMapper;
	
	@Autowired
	private PosApplicationMapper posApplicationMapper;
	
	/**
	 * 导航栏日期
	 * 当前时间：2015-11-16 下午
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("deprecation")
	public String getDate() throws Exception{
		Date date=new Date();
		DateFormat format1=new SimpleDateFormat("yyyy-MM-dd");
		String str=format1.format(date);
		int hh=date.getHours();
		String restr = "";
		if(hh<12){
			restr="当前时间："+str+" 上午 ";
		}
		else
		{
			restr="当前时间："+str+" 下午 ";
		}
		return restr;
	}

	public List<SiteAllInfo> getAllGcList(List<SiteAllInfo> siteList)
			throws Exception {
		List<SiteAllInfo> allGcList = new ArrayList<SiteAllInfo>();
		for(int i=0,len = siteList.size(); i<len; i++){
			SiteAllInfo temp = siteList.get(i);
			int type = temp.getType();
			if(type == 0){
				List<SiteAllInfo> jtList = dataTransInfoMapper.getAllJtSites(temp);
				for(int j=0,length=jtList.size();j<length;j++){
					SiteAllInfo current = jtList.get(j);
					List<SiteAllInfo> gcList = dataTransInfoMapper.getAllGcSitesByJt(current);
					allGcList.addAll(gcList);
				}
			}else if(type == 100){
				List<SiteAllInfo> gcList = dataTransInfoMapper.getAllGcSitesByJt(temp);
				allGcList.addAll(gcList);
			}else if(type == 200){
				List<SiteAllInfo> gcList = dataTransInfoMapper.getAllGcSitesByDy(temp);
				allGcList.addAll(gcList);
			}else if(type == 300){
				// 当用户拥有店铺权限和广场权限时，如果该店铺不属于广场则添加店铺，否则不添加
				int length = allGcList.size();
				boolean flag = false;
				for(int j=0;j<length;j++){
					String siteKey = allGcList.get(j).getSiteid();
					if(siteKey.equals(temp.getSiteid())){
						flag = true;
					}
				}
				if(!flag){
					allGcList.add(temp);
				}
			}
		}
		return allGcList;
	}

	public int getLowPermission(List<SiteAllInfo> siteList) throws Exception {
		int permission = 1000;
		for(int i=0,len = siteList.size();i<len;i++){
			if(siteList.get(i).getType()!= null){
				int currentPermission = siteList.get(i).getType();
				permission = permission < currentPermission ? permission : currentPermission;
			}
		}
		
		return permission;
	}

	public List<SiteAllInfo> getSitesByUser(UserInfo userInfo) throws Exception {
		return dataTransInfoMapper.getSitesByUser(userInfo);
	}

	public List<TransObject> getTransObjects(String para) throws Exception {
		List<WordBook> wordBooks = dataTransInfoMapper.getTransObjects(para);
		List<TransObject> transObjects = new ArrayList<TransObject>();
		for(int i=0,len=wordBooks.size();i<len;i++){
			TransObject transObject = new TransObject();
			WordBook book = wordBooks.get(i);
			if(book != null){
				transObject.setObjectTypeName(book.getDicValue());
				transObject.setObjectTypeId(book.getDicCode());
			}
			transObjects.add(transObject);
		}
		return transObjects;
	}

	public PageDataVo getMappingDataList(PageDataVo pageDataVo)
			throws Exception {
		DataTransInfo transInfo = (DataTransInfo)pageDataVo.getSearchParameter();
		DataTransInfo resultInfo = new DataTransInfo();
		String state = transInfo.getState();
		String objectType = transInfo.getObjectType();
		List<DataTransInfo> returnList = new ArrayList<DataTransInfo>();
		if("300".equals(objectType)){
			if("-1".equals(state)){
				resultInfo = dataTransInfoMapper.selectMappingSquare(transInfo);
				if(resultInfo == null){
					resultInfo = dataTransInfoMapper.selectByCondition(transInfo);
				}
			}else if("0".equals(state)){
				resultInfo = dataTransInfoMapper.selectByCondition(transInfo);
			}else{
				resultInfo = dataTransInfoMapper.selectMappingSquare(transInfo);
			}
			if(resultInfo != null){
				returnList.add(resultInfo);
			}
			pageDataVo.setCurrentPage(1);
			pageDataVo.setMaxPage(1);
			pageDataVo.setMaxRow(1);
			pageDataVo.setPageResult(returnList);
		}else if("6000".equals(objectType)){
			List<DataTransInfo> mappingList = dataTransInfoMapper.queryMappingDataList(pageDataVo);
			List<DataTransInfo> cameraList = dataTransInfoMapper.queryCameraList(pageDataVo);
			int maxRow = dataTransInfoMapper.queryCameraTotal(pageDataVo);
			Set<String> mappingCameraNameSet = new HashSet<String>();
			for(DataTransInfo info : mappingList){
				mappingCameraNameSet.add(info.getInnerid());
			}
			Iterator<DataTransInfo> it = cameraList.iterator();
			while(it.hasNext()){
				if(mappingCameraNameSet.contains(it.next().getInnerid())){
					it.remove();
				}
			}
			
			returnList.addAll(mappingList);
			returnList.addAll(cameraList);
			pageDataVo.setMaxRow(maxRow);
			pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
					/ pageDataVo.getPageSize());
			pageDataVo.setPageResult(returnList);
		}else{
			if("-1".equals(state)){
				// 根据条件查出场所（分页）
				List<DataTransInfo> siteList = dataTransInfoMapper.querySiteList(pageDataVo);
				int maxRow = dataTransInfoMapper.querySiteTotal(pageDataVo);
				// 根据条件查出已映射场所（全部）
				List<DataTransInfo> mappingList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
				
				// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
				Set<String> oriSet = new HashSet<String>();
				Set<String> repeatSet = new HashSet<String>();
				for(int i=0,len=siteList.size();i<len;i++){
					oriSet.add(siteList.get(i).getInnerid());
				}
				Iterator<DataTransInfo> it = mappingList.iterator();
				while(it.hasNext()){
					DataTransInfo temp = it.next();
					if(!oriSet.contains(temp.getInnerid())){
						it.remove();
					}else{
						repeatSet.add(temp.getInnerid());
					}
				}
				
				
				// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
				Iterator<DataTransInfo> siteIt = siteList.iterator();
				while(siteIt.hasNext()){
					DataTransInfo temp = siteIt.next();
					if(repeatSet.contains(temp.getInnerid())){
						siteIt.remove();
					}
				}
				
				pageDataVo.setMaxRow(maxRow);
				returnList.addAll(mappingList);
				returnList.addAll(siteList);
				pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
						/ pageDataVo.getPageSize());
				pageDataVo.setPageResult(returnList);
			}else if("1".equals(state)){
				List<DataTransInfo> mappingList = dataTransInfoMapper.queryMappingDataList(pageDataVo);
				int maxRow = dataTransInfoMapper.queryMappingDataTotal(pageDataVo);
				returnList.addAll(mappingList);
				pageDataVo.setMaxRow(maxRow);
				pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
						/ pageDataVo.getPageSize());
				pageDataVo.setPageResult(returnList);
				return pageDataVo;
			}else{
				// 已映射未使能场所
				List<DataTransInfo> wsnList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
				// 已映射且使能场所
				transInfo.setState("1");
				List<DataTransInfo> snList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
				int snSize = snList.size();
				int maxRow = dataTransInfoMapper.querySiteTotal(pageDataVo) - snSize;
				
				// 根据条件查出场所（分页）
				List<DataTransInfo> siteList = dataTransInfoMapper.querySiteList(pageDataVo);
				
				
				// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
				Set<String> oriSet = new HashSet<String>();
				Set<String> repeatSet = new HashSet<String>();
				for(int i=0,len=siteList.size();i<len;i++){
					oriSet.add(siteList.get(i).getInnerid());
				}
				Iterator<DataTransInfo> wsnIt = wsnList.iterator();
				while(wsnIt.hasNext()){
					DataTransInfo temp = wsnIt.next();
					if(!oriSet.contains(temp.getInnerid())){
						wsnIt.remove();
					}else{
						repeatSet.add(temp.getInnerid());
					}
				}
				
				Iterator<DataTransInfo> snIt = snList.iterator();
				while(snIt.hasNext()){
					DataTransInfo temp = snIt.next();
					if(!oriSet.contains(temp.getInnerid())){
						snIt.remove();
					}else{
						repeatSet.add(temp.getInnerid());
					}
				}
				
				
				// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
				Iterator<DataTransInfo> siteIt = siteList.iterator();
				while(siteIt.hasNext()){
					DataTransInfo temp = siteIt.next();
					if(repeatSet.contains(temp.getInnerid())){
						siteIt.remove();
					}
				}
				
				
				returnList.addAll(wsnList);
				returnList.addAll(siteList);
				pageDataVo.setMaxRow(maxRow);
				pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
						/ pageDataVo.getPageSize());
				pageDataVo.setPageResult(returnList);
			}
		}
		
		return pageDataVo;
	}
	
	
	public PageDataVo getMappingDataListDetail(PageDataVo pageDataVo)
				throws Exception {
			DataTransInfo transInfo = (DataTransInfo)pageDataVo.getSearchParameter();
			DataTransInfo resultInfo = new DataTransInfo();
			String objectType = transInfo.getObjectType();
			List<DataTransInfo> returnList = new ArrayList<DataTransInfo>();
			String parentType = transInfo.getParentType();
			String state = transInfo.getState();
			if("300".equals(objectType)){
				resultInfo = dataTransInfoMapper.selectMappingSquare(transInfo);
				if(resultInfo == null && "-1".equals(state)){
					resultInfo = dataTransInfoMapper.selectByCondition(transInfo);
				}
				if(resultInfo!=null){
					returnList.add(resultInfo);
				}
				pageDataVo.setCurrentPage(1);
				pageDataVo.setMaxPage(1);
				pageDataVo.setMaxRow(1);
				pageDataVo.setPageResult(returnList);
			}else if("6000".equals(objectType)){
				List<DataTransInfo> mappingList = dataTransInfoMapper.queryMappingDataList(pageDataVo);
				List<DataTransInfo> cameraList = dataTransInfoMapper.queryCameraList(pageDataVo);
				int maxRow = dataTransInfoMapper.queryCameraTotal(pageDataVo);
				Set<String> mappingCameraNameSet = new HashSet<String>();
				for(DataTransInfo info : mappingList){
					mappingCameraNameSet.add(info.getInnerid());
				}
				Iterator<DataTransInfo> it = cameraList.iterator();
				while(it.hasNext()){
					if(mappingCameraNameSet.contains(it.next().getInnerid())){
						it.remove();
					}
				}
				
				returnList.addAll(mappingList);
				returnList.addAll(cameraList);
				pageDataVo.setMaxRow(maxRow);
				pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
						/ pageDataVo.getPageSize());
				pageDataVo.setPageResult(returnList);
			}else{
				List<DataTransInfo> mappingList = new ArrayList<DataTransInfo>();;
				int pageSize = pageDataVo.getPageSize();
				int maxRow = 0;
				List<DataTransInfo> siteList = new ArrayList<DataTransInfo>();
				if("300".equals(parentType)){
					if("-1".equals(state)){
						// 根据条件查出场所（分页）
						siteList = dataTransInfoMapper.querySiteList(pageDataVo);
						maxRow = dataTransInfoMapper.querySiteTotal(pageDataVo);
						// 根据条件查出已映射场所（全部）
						mappingList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						
						// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
						Set<String> oriSet = new HashSet<String>();
						Set<String> repeatSet = new HashSet<String>();
						for(int i=0,len=siteList.size();i<len;i++){
							oriSet.add(siteList.get(i).getInnerid());
						}
						Iterator<DataTransInfo> it = mappingList.iterator();
						while(it.hasNext()){
							DataTransInfo temp = it.next();
							if(!oriSet.contains(temp.getInnerid())){
								it.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						
						// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
						Iterator<DataTransInfo> siteIt = siteList.iterator();
						while(siteIt.hasNext()){
							DataTransInfo temp = siteIt.next();
							if(repeatSet.contains(temp.getInnerid())){
								siteIt.remove();
							}
						}
						
						pageDataVo.setMaxRow(maxRow);
						returnList.addAll(mappingList);
						returnList.addAll(siteList);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					}else if("1".equals(state)){
						mappingList = dataTransInfoMapper.queryMappingDataList(pageDataVo);
						maxRow = dataTransInfoMapper.queryMappingDataTotal(pageDataVo);
						returnList.addAll(mappingList);
						pageDataVo.setMaxRow(maxRow);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					}else{
						// 已映射未使能场所
						List<DataTransInfo> wsnList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						// 已映射且使能场所
						transInfo.setState("1");
						List<DataTransInfo> snList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						int snSize = snList.size();
						maxRow = dataTransInfoMapper.querySiteTotal(pageDataVo) - snSize;
						
						// 根据条件查出场所（分页）
						siteList = dataTransInfoMapper.querySiteList(pageDataVo);
						
						
						// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
						Set<String> oriSet = new HashSet<String>();
						Set<String> repeatSet = new HashSet<String>();
						for(int i=0,len=siteList.size();i<len;i++){
							oriSet.add(siteList.get(i).getInnerid());
						}
						Iterator<DataTransInfo> wsnIt = wsnList.iterator();
						while(wsnIt.hasNext()){
							DataTransInfo temp = wsnIt.next();
							if(!oriSet.contains(temp.getInnerid())){
								wsnIt.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						Iterator<DataTransInfo> snIt = snList.iterator();
						while(snIt.hasNext()){
							DataTransInfo temp = snIt.next();
							if(!oriSet.contains(temp.getInnerid())){
								snIt.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						
						// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
						Iterator<DataTransInfo> siteIt = siteList.iterator();
						while(siteIt.hasNext()){
							DataTransInfo temp = siteIt.next();
							if(repeatSet.contains(temp.getInnerid())){
								siteIt.remove();
							}
						}
						
						
						returnList.addAll(wsnList);
						returnList.addAll(siteList);
						pageDataVo.setMaxRow(maxRow);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					}
					
				}else if("400".equals(parentType)){
					if("-1".equals(state)){
						// 根据条件查出场所（分页）
						siteList = dataTransInfoMapper.querySiteListInQy(pageDataVo);
						maxRow = dataTransInfoMapper.querySiteListInQyTotal(pageDataVo);
						// 根据条件查出已映射场所（全部）
						mappingList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						
						// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
						Set<String> oriSet = new HashSet<String>();
						Set<String> repeatSet = new HashSet<String>();
						for(int i=0,len=siteList.size();i<len;i++){
							oriSet.add(siteList.get(i).getInnerid());
						}
						Iterator<DataTransInfo> it = mappingList.iterator();
						while(it.hasNext()){
							DataTransInfo temp = it.next();
							if(!oriSet.contains(temp.getInnerid())){
								it.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						
						// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
						Iterator<DataTransInfo> siteIt = siteList.iterator();
						while(siteIt.hasNext()){
							DataTransInfo temp = siteIt.next();
							if(repeatSet.contains(temp.getInnerid())){
								siteIt.remove();
							}
						}
						
						pageDataVo.setMaxRow(maxRow);
						returnList.addAll(mappingList);
						returnList.addAll(siteList);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					}else if("1".equals(state)){

						// 使能场所
						mappingList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						siteList = dataTransInfoMapper.querySiteListInQy(pageDataVo);
						maxRow = dataTransInfoMapper.querySiteListInQyTotal(pageDataVo);
						Set<String> mappingSet = new HashSet<String>();
						for(int i=0,len=mappingList.size();i<len;i++){
							mappingSet.add(mappingList.get(i).getInnerid());
						}
						
						// 找出siteList里面的已映射场所
						Iterator<DataTransInfo> it = siteList.iterator();
						while(it.hasNext()){
							DataTransInfo temp = it.next();
							if(!mappingSet.contains(temp.getInnerid())){
								it.remove();
							}
						}
						
						Set<String> siteSet = new HashSet<String>();
						for(int i=0;i<siteList.size();i++){
							siteSet.add(siteList.get(i).getInnerid());
						}
						
						Iterator<DataTransInfo> ite = mappingList.iterator();
						while(ite.hasNext()){
							DataTransInfo temp = ite.next();
							if(!siteSet.contains(temp.getInnerid())){
								ite.remove();
							}
						}
						
						pageDataVo.setPageSize(pageSize);
						returnList.addAll(mappingList);
						pageDataVo.setMaxRow(maxRow);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					}else{
						// 已映射未使能场所
						List<DataTransInfo> wsnList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						// 已映射且使能场所
						transInfo.setState("1");
						List<DataTransInfo> snList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						int snSize = snList.size();
						maxRow = dataTransInfoMapper.querySiteListInQyTotal(pageDataVo) - snSize;
						
						// 根据条件查出场所（分页）
						siteList = dataTransInfoMapper.querySiteListInLc(pageDataVo);
						
						
						// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
						Set<String> oriSet = new HashSet<String>();
						Set<String> repeatSet = new HashSet<String>();
						for(int i=0,len=siteList.size();i<len;i++){
							oriSet.add(siteList.get(i).getInnerid());
						}
						Iterator<DataTransInfo> wsnIt = wsnList.iterator();
						while(wsnIt.hasNext()){
							DataTransInfo temp = wsnIt.next();
							if(!oriSet.contains(temp.getInnerid())){
								wsnIt.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						Iterator<DataTransInfo> snIt = snList.iterator();
						while(snIt.hasNext()){
							DataTransInfo temp = snIt.next();
							if(!oriSet.contains(temp.getInnerid())){
								snIt.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						
						// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
						Iterator<DataTransInfo> siteIt = siteList.iterator();
						while(siteIt.hasNext()){
							DataTransInfo temp = siteIt.next();
							if(repeatSet.contains(temp.getInnerid())){
								siteIt.remove();
							}
						}
						
						
						returnList.addAll(wsnList);
						returnList.addAll(siteList);
						pageDataVo.setMaxRow(maxRow);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					
					}
				}else if("500".equals(parentType)){
					if("-1".equals(state)){
						// 根据条件查出场所（分页）
						siteList = dataTransInfoMapper.querySiteListInLc(pageDataVo);
						maxRow = dataTransInfoMapper.querySiteListInLcTotal(pageDataVo);
						// 根据条件查出已映射场所（全部）
						mappingList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						
						// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
						Set<String> oriSet = new HashSet<String>();
						Set<String> repeatSet = new HashSet<String>();
						for(int i=0,len=siteList.size();i<len;i++){
							oriSet.add(siteList.get(i).getInnerid());
						}
						Iterator<DataTransInfo> it = mappingList.iterator();
						while(it.hasNext()){
							DataTransInfo temp = it.next();
							if(!oriSet.contains(temp.getInnerid())){
								it.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						
						// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
						Iterator<DataTransInfo> siteIt = siteList.iterator();
						while(siteIt.hasNext()){
							DataTransInfo temp = siteIt.next();
							if(repeatSet.contains(temp.getInnerid())){
								siteIt.remove();
							}
						}
						
						pageDataVo.setMaxRow(maxRow);
						returnList.addAll(mappingList);
						returnList.addAll(siteList);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					}else if("1".equals(state)){

						// 使能场所
						mappingList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						siteList = dataTransInfoMapper.querySiteListInLc(pageDataVo);
						maxRow = dataTransInfoMapper.querySiteListInLcTotal(pageDataVo);
						Set<String> mappingSet = new HashSet<String>();
						for(int i=0,len=mappingList.size();i<len;i++){
							mappingSet.add(mappingList.get(i).getInnerid());
						}
						
						// 找出siteList里面的已映射场所
						Iterator<DataTransInfo> it = siteList.iterator();
						while(it.hasNext()){
							DataTransInfo temp = it.next();
							if(!mappingSet.contains(temp.getInnerid())){
								it.remove();
							}
						}
						
						Set<String> siteSet = new HashSet<String>();
						for(int i=0;i<siteList.size();i++){
							siteSet.add(siteList.get(i).getInnerid());
						}
						
						Iterator<DataTransInfo> ite = mappingList.iterator();
						while(ite.hasNext()){
							DataTransInfo temp = ite.next();
							if(!siteSet.contains(temp.getInnerid())){
								ite.remove();
							}
						}
						
						pageDataVo.setPageSize(pageSize);
						returnList.addAll(mappingList);
						pageDataVo.setMaxRow(maxRow);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					}else{
						// 已映射未使能场所
						List<DataTransInfo> wsnList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						// 已映射且使能场所
						transInfo.setState("1");
						List<DataTransInfo> snList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						int snSize = snList.size();
						maxRow = dataTransInfoMapper.querySiteListInLcTotal(pageDataVo) - snSize;
						
						// 根据条件查出场所（分页）
						siteList = dataTransInfoMapper.querySiteListInLc(pageDataVo);
						
						
						// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
						Set<String> oriSet = new HashSet<String>();
						Set<String> repeatSet = new HashSet<String>();
						for(int i=0,len=siteList.size();i<len;i++){
							oriSet.add(siteList.get(i).getInnerid());
						}
						Iterator<DataTransInfo> wsnIt = wsnList.iterator();
						while(wsnIt.hasNext()){
							DataTransInfo temp = wsnIt.next();
							if(!oriSet.contains(temp.getInnerid())){
								wsnIt.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						Iterator<DataTransInfo> snIt = snList.iterator();
						while(snIt.hasNext()){
							DataTransInfo temp = snIt.next();
							if(!oriSet.contains(temp.getInnerid())){
								snIt.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						
						// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
						Iterator<DataTransInfo> siteIt = siteList.iterator();
						while(siteIt.hasNext()){
							DataTransInfo temp = siteIt.next();
							if(repeatSet.contains(temp.getInnerid())){
								siteIt.remove();
							}
						}
						
						
						returnList.addAll(wsnList);
						returnList.addAll(siteList);
						pageDataVo.setMaxRow(maxRow);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					
					}
				}else if("600".equals(parentType)){
					if("-1".equals(state)){
						// 根据条件查出场所（分页）
						siteList = dataTransInfoMapper.querySiteListInDp(pageDataVo);
						maxRow = dataTransInfoMapper.querySiteListInDpTotal(pageDataVo);
						// 根据条件查出已映射场所（全部）
						mappingList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						
						// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
						Set<String> oriSet = new HashSet<String>();
						Set<String> repeatSet = new HashSet<String>();
						for(int i=0,len=siteList.size();i<len;i++){
							oriSet.add(siteList.get(i).getInnerid());
						}
						Iterator<DataTransInfo> it = mappingList.iterator();
						while(it.hasNext()){
							DataTransInfo temp = it.next();
							if(!oriSet.contains(temp.getInnerid())){
								it.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						
						// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
						Iterator<DataTransInfo> siteIt = siteList.iterator();
						while(siteIt.hasNext()){
							DataTransInfo temp = siteIt.next();
							if(repeatSet.contains(temp.getInnerid())){
								siteIt.remove();
							}
						}
						
						pageDataVo.setMaxRow(maxRow);
						returnList.addAll(mappingList);
						returnList.addAll(siteList);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					}else if("1".equals(state)){

						// 使能场所
						mappingList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						siteList = dataTransInfoMapper.querySiteListInDp(pageDataVo);
						maxRow = dataTransInfoMapper.querySiteListInDpTotal(pageDataVo);
						Set<String> mappingSet = new HashSet<String>();
						for(int i=0,len=mappingList.size();i<len;i++){
							mappingSet.add(mappingList.get(i).getInnerid());
						}
						
						// 找出siteList里面的已映射场所
						Iterator<DataTransInfo> it = siteList.iterator();
						while(it.hasNext()){
							DataTransInfo temp = it.next();
							if(!mappingSet.contains(temp.getInnerid())){
								it.remove();
							}
						}
						
						Set<String> siteSet = new HashSet<String>();
						for(int i=0;i<siteList.size();i++){
							siteSet.add(siteList.get(i).getInnerid());
						}
						
						Iterator<DataTransInfo> ite = mappingList.iterator();
						while(ite.hasNext()){
							DataTransInfo temp = ite.next();
							if(!siteSet.contains(temp.getInnerid())){
								ite.remove();
							}
						}
						
						pageDataVo.setPageSize(pageSize);
						returnList.addAll(mappingList);
						pageDataVo.setMaxRow(maxRow);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					}else{
						// 已映射未使能场所
						List<DataTransInfo> wsnList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						// 已映射且使能场所
						transInfo.setState("1");
						List<DataTransInfo> snList = dataTransInfoMapper.queryMappingDataAll(pageDataVo);
						int snSize = snList.size();
						maxRow = dataTransInfoMapper.querySiteListInDpTotal(pageDataVo) - snSize;
						
						// 根据条件查出场所（分页）
						siteList = dataTransInfoMapper.querySiteListInDp(pageDataVo);
						
						
						// 获得一个分页场所innerId的一个set集，用该set集找出映射表里相应的场所
						Set<String> oriSet = new HashSet<String>();
						Set<String> repeatSet = new HashSet<String>();
						for(int i=0,len=siteList.size();i<len;i++){
							oriSet.add(siteList.get(i).getInnerid());
						}
						Iterator<DataTransInfo> wsnIt = wsnList.iterator();
						while(wsnIt.hasNext()){
							DataTransInfo temp = wsnIt.next();
							if(!oriSet.contains(temp.getInnerid())){
								wsnIt.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						Iterator<DataTransInfo> snIt = snList.iterator();
						while(snIt.hasNext()){
							DataTransInfo temp = snIt.next();
							if(!oriSet.contains(temp.getInnerid())){
								snIt.remove();
							}else{
								repeatSet.add(temp.getInnerid());
							}
						}
						
						
						// 根据映射innerId的set集删除分页场所里的场所，并以映射场所取代之
						Iterator<DataTransInfo> siteIt = siteList.iterator();
						while(siteIt.hasNext()){
							DataTransInfo temp = siteIt.next();
							if(repeatSet.contains(temp.getInnerid())){
								siteIt.remove();
							}
						}
						
						
						returnList.addAll(wsnList);
						returnList.addAll(siteList);
						pageDataVo.setMaxRow(maxRow);
						pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
								/ pageDataVo.getPageSize());
						pageDataVo.setPageResult(returnList);
						return pageDataVo;
					
					}
				}
				Set<String> mappingCameraNameSet = new HashSet<String>();
				for(DataTransInfo info : mappingList){
					mappingCameraNameSet.add(info.getInnerid());
				}
				Iterator<DataTransInfo> it = siteList.iterator();
				while(it.hasNext()){
					if(mappingCameraNameSet.contains(it.next().getInnerid())){
						it.remove();
					}
				}
				
				pageDataVo.setPageSize(pageSize);
				returnList.addAll(mappingList);
				returnList.addAll(siteList);
				pageDataVo.setMaxRow(maxRow);
				pageDataVo.setMaxPage((maxRow + pageDataVo.getPageSize() - 1)
						/ pageDataVo.getPageSize());
				pageDataVo.setPageResult(returnList);
			}
			
			return pageDataVo;
		}
	

	public Map saveMappingData(DataTransInfo dataTransInfo) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		dataTransInfoMapper.insert(dataTransInfo);
		map.put("infoId", dataTransInfo.getId());
		// 插入日志表
		return map;
	}
	
	
	public Map deleteMappingData(String id) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		dataTransInfoMapper.deleteMappingData(id);
		// 插入日志表
		return map;
	}

	public MessageCommon updateMappingData(DataTransInfo dataTransInfo) throws Exception {
		MessageCommon message = new MessageCommon();
		dataTransInfoMapper.updateByPrimaryKey(dataTransInfo);
		message.setFlag(true);
		message.setMessage("保存映射数据成功！");
		// 插入日志表
		return message;
	}

	public List<TableColumnInfo> getColumnNames(String tableName) throws Exception {
		return dataTransInfoMapper.getColumnNames(tableName);
	}

	public MessageCommon saveTemplateColumn(PosApplication info) throws Exception {
		MessageCommon message = new MessageCommon();
		posApplicationMapper.insert(info);
		message.setFlag(true);
		message.setMessage("保存映射数据成功！");
		// 插入日志表
		return message;
	}
	
	public MessageCommon deleteTemplateColumn(PosApplication info) throws Exception {
		MessageCommon message = new MessageCommon();
		posApplicationMapper.deleteByCondition(info);
		message.setFlag(true);
		message.setMessage("删除映射数据成功！");
		// 插入日志表
		return message;
	}

	public List<PosApplication> getTemplateColumns(PosApplication info)
			throws Exception {
		return dataTransInfoMapper.getTemplateColumns(info);
	}


	public MessageCommon updateTemplateColumn(PosApplication info)
			throws Exception {
		MessageCommon message = new MessageCommon();
		posApplicationMapper.updateTemplateColumn(info);
		message.setFlag(true);
		message.setMessage("保存映射数据成功！");
		// 插入日志表
		return message;
	}

	public String getSiteNameById(String siteId) throws Exception {
		return dataTransInfoMapper.getSiteNameById(siteId);
	}

	public List<DataTransInfo> getQyInDp(String squareId) throws Exception {
		return dataTransInfoMapper.getQyInDp(squareId);
	}

	public List<DataTransInfo> getQyInGc(String squareId) throws Exception {
		return dataTransInfoMapper.getQyInGc(squareId);
	}

	public List<DataTransInfo> getQyInLc(String squareId) throws Exception {
		return dataTransInfoMapper.getQyInLc(squareId);
	}

	public List<DataTransInfo> getQyInQy(String squareId) throws Exception {
		return dataTransInfoMapper.getQyInQy(squareId);
	}
	
	public List<DataTransInfo> getLcInDp(String squareId) throws Exception {
		return dataTransInfoMapper.getLcInDp(squareId);
	}

	public List<DataTransInfo> getLcInGc(String squareId) throws Exception {
		return dataTransInfoMapper.getLcInGc(squareId);
	}

	public List<DataTransInfo> getLcInQy(String squareId) throws Exception {
		return dataTransInfoMapper.getLcInQy(squareId);
	}
	
	public List<DataTransInfo> getDpInLc(String squareId) throws Exception {
		return dataTransInfoMapper.getDpInLc(squareId);
	}

	public List<DataTransInfo> getDpInGc(String squareId) throws Exception {
		return dataTransInfoMapper.getDpInGc(squareId);
	}

	public List<DataTransInfo> getDpInQy(String squareId) throws Exception {
		return dataTransInfoMapper.getDpInQy(squareId);
	}

	public List<DataTransInfo> getTdInDp(String squareId) throws Exception {
		return dataTransInfoMapper.getTdInDp(squareId);
	}

	public List<DataTransInfo> getTdInGc(String squareId) throws Exception {
		return dataTransInfoMapper.getTdInGc(squareId);
	}

	public List<DataTransInfo> getTdInLc(String squareId) throws Exception {
		return dataTransInfoMapper.getTdInLc(squareId);
	}

	public List<DataTransInfo> getTdInQy(String squareId) throws Exception {
		return dataTransInfoMapper.getTdInQy(squareId);
	}

	public List<DataTransInfo> getNameListBySiteType(DataTransInfo info)
			throws Exception {
		String squareId = info.getSquare();
		String siteType = info.getParentType();
		if("400".equals(siteType)){
			return dataTransInfoMapper.getParentQyInGc(squareId);
		}else if("500".equals(siteType)){
			return dataTransInfoMapper.getParentLcInGc(squareId);
		}else if("600".equals(siteType)){
			return dataTransInfoMapper.getParentDpInGc(squareId);
		}
		return null;
	}

	public List<DataTransInfo> getSiteInDp(DataTransInfo info) throws Exception {
		return dataTransInfoMapper.getSiteInDp(info);
	}

	public List<DataTransInfo> getSiteInGc(DataTransInfo info) throws Exception {
		return dataTransInfoMapper.getSiteInGc(info);
	}

	public List<DataTransInfo> getSiteInLc(DataTransInfo info) throws Exception {
		return dataTransInfoMapper.getSiteInLc(info);
	}

	public List<DataTransInfo> getSiteInQy(DataTransInfo info) throws Exception {
		return dataTransInfoMapper.getSiteInQy(info);
	}

	public List<DataTransInfo> getMappingData(DataTransInfo info)
			throws Exception {
		return dataTransInfoMapper.getMappingData(info);
	}

	public DataTransInfo getInfoByIdAndType(DataTransInfo info)
			throws Exception {
		return dataTransInfoMapper.getInfoByIdAndType(info);
	}

	public Map makeAllFiltered(DataTransInfo info) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		dataTransInfoMapper.updateBySelective(info);
		return map;
	}

	public Map makeById(DataTransInfo info) throws Exception {
		Map<String,Object> map = new HashMap<String,Object>();
		dataTransInfoMapper.updateByPrimaryKey(info);
		return map;
	}

	public List<DataTransInfo> getMappingSnData(DataTransInfo info)
			throws Exception {
		return dataTransInfoMapper.getMappingSnData(info);
	}


}
